// pages/new-room-page/new-room-page.js
Page({
    data: {
        showLoadingState:false,
        //运动图标
        sportsNum:0,
        sportsArray:['所有项目','足球','篮球','排球','羽毛球','乒乓球','棒球','网球'],
        sportsPicUrlArray:["https://pic.imgdb.cn/item/62f6051516f2c2beb1dd42b3.png",
        "https://pic.imgdb.cn/item/62f6048616f2c2beb1db8024.png",
        "https://pic.imgdb.cn/item/62f6048616f2c2beb1db802b.png",
        "https://pic.imgdb.cn/item/62f6048616f2c2beb1db8047.png",
        "https://pic.imgdb.cn/item/62f6048616f2c2beb1db800a.png",
        "https://pic.imgdb.cn/item/62f6053716f2c2beb1ddabbd.png",
        "https://pic.imgdb.cn/item/62f6053716f2c2beb1ddabea.png",
        "https://pic.imgdb.cn/item/62f6053716f2c2beb1ddabba.png",],
        //选择location和playground
        p2:"https://pic.imgdb.cn/item/62e2cf02f54cd3f937ebf183.png",
        locationsArray:['请选择你的运动地点'],
        playgroundsArray:['请选择你的运动场地'],
        locationNum:0,
        playgroundNum:0,
        //选择日期
        dateArray:['请选择日期','2000-1-1','2000-1-2'],
        dateNum:0,
        //运动人数//array.index + 1 = personNum
        personArray:['请选择参加人数','二人','三人','四人','五人','六人','七人','八人','九人','十人','十一人','十二人'],
        personNum:0,
        //选择时间 不能选0 可选1 已选2          time.index + 7为时间
        timeArray:[0,1,1,1,1,1,1,1,1,1,1,1,1],
        timeSelectNum:0,
        initTimeArray:[0,1,1,1,1,1,1,1,1,1,1,1,1],
        starttime:8,
        endtime:9,
        p3:"https://pic1.imgdb.cn/item/635b9d6916f2c2beb12009b5.png",//灰
        p4:"https://pic1.imgdb.cn/item/635b9d6916f2c2beb12009bb.png",//橙色
        p5:"https://pic1.imgdb.cn/item/635b9d6916f2c2beb12009ca.png",//青色
        p6:"https://pic1.imgdb.cn/item/635b9d6916f2c2beb12009d6.png",//背景
        p7:"https://pic1.imgdb.cn/item/635b9d6916f2c2beb12009df.png",//下标
        //创建房间按钮
        p1:"https://pic.imgdb.cn/item/62e2cf02f54cd3f937ebf177.png",
    },
//     * 生命周期函数--监听页面加载
    onLoad(options) { 
        this.setData({
            [`sportsNum`]:options.sportsNum,
        });
        //进入页面如果选择了运动，则刷新locationArray
        if(options.sportsNum != 0){
            var that = this;
            that.onShowLoading();
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:10000,
                data:{
                  operation:"GET_LOCATIONARRAY",
                  sportsNum:that.data.sportsNum,
                },
                success(res){
                  if(res.data == false){
                      that.onHideLoading()
                      wx.showModal({
                          showCancel:false,
                          title:"请求服务器失败,请重试",
                      });
                  }
                  else{
                      //成功获取到信息后的操作
                      that.onHideLoading
                      that.setData({
                          'locationsArray':res.data.arr,
                      });
                  }
                },
                fail(res){
                    that.onHideLoading();
                  wx.showModal({
                      showCancel:false,
                      title:"无法获取到运动地点，请重新选择运动并重试",
                  });
                },
              });
        };
        //设置日期的值(获取今天和明天的日期)
        var date = new Date();
        var today = date.getFullYear() + '-' +(date.getMonth()+1) + '-' + date.getDate();
        
        date.setTime(date.getTime()+24*60*60*1000);
        var tomorrow = date.getFullYear() + '-' +(date.getMonth()+1) + '-' + date.getDate();
        
                
        this.setData({
            'dateArray[1]':today,
            'dateArray[2]':tomorrow,
        });

    },
    //运动图标
    onChangeSports:function(e){
        var x = e.detail.value;
        this.setData({
            "sportsNum":x,
            "locationNum":0,
            "playgroundNum":0,
        })
        //从服务器获取后修改locationsArray
        var that = this;
        that.onShowLoading();
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              operation:"GET_LOCATIONARRAY",
              sportsNum:that.data.sportsNum,
            },
            success(res){
                that.onHideLoading();
              if(res.data == false){
                  wx.showModal({
                      showCancel:false,
                      title:"请求服务器失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  that.setData({
                      'locationsArray':res.data.arr,
                  });
              }
            },
            fail(res){
                that.onHideLoading();
              wx.showModal({
                  showCancel:false,
                  title:"无法获取到运动地点，请重新选择运动并重试",
              });
            },
          });
    },
    //选择location和playground
    onChangeLocation:function(e){
        if(this.data.sportsNum == 0){
            //没有选择运动项目要先选择运动项目
            wx.showModal({
              showCancel:false,
              title:"请先选择运动项目",
            })
        }
        else
        {
            var x = e.detail.value;
            this.setData({
                'locationNum':x,
                "playgroundNum":0,
            })
            //若日期也已经选择，则从服务器获取后修改playgroundsArray
            var that = this;
            that.onShowLoading();
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:10000,
                data:{
                  operation:"GET_PLAYGROUNDARRAY",
                  sportsNum:that.data.sportsNum,
                  locationsNum:that.data.locationsArray[that.data.locationNum],
                },
                success(res){
                    that.onHideLoading();
                  if(res.data == false){

                      wx.showModal({
                          showCancel:false,
                          title:"请求失败,请重试",
                      });
                  }
                  else{
                      //成功获取到信息后的操作
                      that.setData({
                          'playgroundsArray':res.data.arr,
                      })
                  }
                },
                fail(res){
                    that.onHideLoading();
                  wx.showModal({
                      showCancel:false,
                      title:"请求超时,请重试",
                  });
                },
              });
        }
    },
    onChangePlayground:function(e){
        if(this.data.locationNum == 0){
            wx.showModal({
              showCancel:false,
              title:"请先选择运动地点",
            })
        }
        else if(this.data.dateNum == 0){
            wx.showModal({
                showCancel:false,
                title:"请先选择日期",
              })
        }
        else{
            var x = e.detail.value;
            this.setData({
                'playgroundNum':x,
            })
            //向服务器传递信息，获取time array
            var that = this;
            that.onShowLoading();
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:100000,
                data:{
                  //各种数据的传送
                  operation:"GET_TIMEARRAY",
                  date:that.data.dateArray[that.data.dateNum],
                  locationNum:that.data.locationsArray[that.data.locationNum],
                  playgroundsNum:that.data.playgroundsArray[that.data.playgroundNum],
                },
                success(res){
                    that.onHideLoading();
                  if(res.data == false){
                      wx.showModal({
                          showCancel:false,
                          title:"请求失败,请重试",
                      });
                  }
                  else{
                      //成功获取到信息后的操作
                      console.log(res);
                      console.log({
                        //各种数据的传送
                        operation:"GET_TIMEARRAY",
                        date:that.data.dateArray[that.data.dateNum],
                        locationNum:that.data.locationsArray[that.data.locationNum],
                        playgroundsNum:that.data.playgroundsArray[that.data.playgroundNum],
                      },);
                      console.log(that.data.locationsArray);
                      that.setData({
                          'timeArray':res.data.arr.splice(8,13),
                      });
                      var arrar = [];
                      that.setData({
                        'initTimeArray':arrar.concat(that.data.timeArray),
                    });
                  }
                },
                fail(res){
                    that.onHideLoading();
                  wx.showModal({
                      showCancel:false,
                      title:"请求超时,请重试",
                  });
                },
              });
        }

    },
    //选择日期
    onChangeDate:function(e){
        var x = e.detail.value;
        this.setData({
            'dateNum':x,
        })
    },
    //运动人数
    onChangePerson:function(e){
        var x = e.detail.value;
        this.setData({
            'personNum':x,
        })
    },
    //选择时间
    onChangeTime:function(e){
        var that = this;
        if(this.data.playgroundNum == 0){
            wx.showModal({
              showCancel:false,
              title:"请先选择运动地点",
            })
        }
        else if(this.data.dateNum == 0){
            wx.showModal({
              showCancel:false,
              title:"请先选择运动日期",
            })
        }
        else
        {
            var index = e.currentTarget.dataset.index;
            if(this.data.timeArray[index] == 1){
                //首先要判断是第几个选择 0的话直接选 1的话判断是否在已选择的旁边后再选 2的话初始化并弹窗提示
                var that = this;
                var num = that.data.timeSelectNum;
                switch(num){
                    case 0:
                        this.setData({
                            [`timeArray[${index}]`]: 2,
                            'timeSelectNum':1,
                            'starttime':index + 8,
                            'endtime':index + 9,
                        }); 
                        break;
                    case 1:
                        if(index != 0 && index != 12){
                            if(that.data.timeArray[index - 1] == 2){
                                this.setData({
                                    [`timeArray[${index}]`]: 2,
                                    "timeSelectNum":2,
                                    'endtime':that.data.endtime + 1,
                                });
                            }
                            else if(that.data.timeArray[index + 1] == 2){
                                this.setData({
                                    [`timeArray[${index}]`]: 2,
                                    "timeSelectNum":2,
                                    'starttime':that.data.starttime - 1,
                                });
                            }
                            else{
                                wx.showModal({
                                    showCancel:false,
                                    title:"只能选择连续时间段，请重新选择",
                                })
                                that.onInitTimeArray();
                            };
                        }
                        else if(index == 0){
                            if(that.data.timeArray[index + 1] == 2){
                                this.setData({
                                    [`timeArray[${index}]`]: 2,
                                    "timeSelectNum":2,
                                    'starttime':that.data.starttime - 1,
                                });
                            }
                            else{
                                wx.showModal({
                                    showCancel:false,
                                    title:"只能选择连续时间段，请重新选择",
                                })
                                that.onInitTimeArray();
                            };
                        }
                        else if(index == 12){
                            if(that.data.timeArray[index - 1] == 2){
                                this.setData({
                                    [`timeArray[${index}]`]: 2,
                                    "timeSelectNum":2,
                                    'endtime':that.data.endtime + 1,
                                });
                            }
                            else{
                                wx.showModal({
                                    showCancel:false,
                                    title:"只能选择连续时间段，请重新选择",
                                })
                                that.onInitTimeArray();
                            };
                        };
                        break;
                    case 2:
                        that.onInitTimeArray();
                        wx.showModal({
                          showCancel:false,
                          title:"最多只能选择两个时间段，请重新选择",
                        })
                        break;

                }
            }
            else if(that.data.timeArray[index] == 2){
                if(that.data.timeSelectNum == 2){
                    if(index == 0){
                        that.setData({
                            "starttime":that.data.starttime + 1,
                        })
                    }
                    else if(index == 12){
                        that.setData({
                            "endtime":that.data.endtime - 1,
                        })
                    }
                    else{
                        if(that.data.timeArray[index - 1] == 2){
                            that.setData({
                                "endtime":that.data.endtime - 1,
                            })
                        }
                        else if(that.data.timeArray[index + 1] == 2){
                            that.setData({
                                "starttime":that.data.starttime + 1,
                            })
                        }
                    };
                }
                    that.setData({
                        [`timeArray[${index}]`]: 1,
                        "timeSelectNum":that.data.timeSelectNum - 1,
                    });
            };
            console.log(that.data.starttime +" "+ that.data.endtime)
        }
    },
    //初始化时间(在选择超过两个动作以后)
    onInitTimeArray:function(e){
        var initArray = [];
        this.setData({
            'timeArray':initArray.concat(this.data.initTimeArray),
            'timeSelectNum':0,
            "starttime":8,
            "endtime":10,
        })
    },
    //跳转到新页面  还未判断未选时间的情况
    toPage_detailed_room:function(e){
        if(this.data.locationNum == 0||
            this.data.personNum == 0||
            this.data.playgroundNum == 0||
            this.data.dateNum == 0||
            this.data.sportsNum == 0
            ){
                wx.showModal({
                    title:"您还有未选择的项目，请完成选择",
                    showCancel:false,
                  })
        }
        else{
            //先向服务器传递信息，然后获取到roomid后，跳转到新页面
            {
                var that = this;
                that.onShowLoading()
                wx.request({
                    url: getApp().globalData.serverUrl,
                    method:"GET",
                    timeout:10000,
                    data:{
                      //各种数据的传送
                      operation:"CREATE_ROOM",
                      hostId:getApp().globalData.userInfo.studentId,
                      sportsNum:that.data.sportsNum,
                      locationsNum:that.data.locationsArray[that.data.locationNum],
                      date:that.data.dateArray[that.data.dateNum],
                      playgroundsNum:that.data.playgroundsArray[that.data.playgroundNum],
                      personNum:parseInt(that.data.personNum)+1,
                      starttime:that.data.starttime,
                      endtime:that.data.endtime,
                    },
                    success(res){
                      if(res.data == false){
                          that.onHideLoading()
                          wx.showModal({
                              showCancel:false,
                              title:"请求失败,请重试",
                          });
                      }
                      else{
                          //成功获取到信息后的操作\
                          that.onHideLoading()
                          var roomid = res.data;
                          wx.navigateTo({
                            url: '/pages/detailed-room-page/detailed-room-page?roomid='+roomid,
                          })
                      }
                    },
                    fail(res){
                        that.onHideLoading();
                      wx.showModal({
                          showCancel:false,
                          title:"请求超时,请重试",
                      });
                    },
                  });
            }
            
        }
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})